﻿Imports System.Data.OleDb
Imports System.Xml

Public Class rss
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'RSS can be understand as Really Simple Syndication. To creat the RSS, it require XML file. In other word,
        'people has to follow the XML formate to creat a RSS.
        'The structure for my RSS is looks like this
        '<?xml version="1.0" encoding="UTF-8"?>
        '<rss version="2.0">
        '   <channel>
        '       <title>Chinese Spirit and Liquired</title>
        '       <link>http://localhost:58219/List.aspx</title>
        '       <description>Enjoy youself with this website, Thank you!</description>
        '       <language>en-uk</language>
        '       <item>
        '           <title>title of each spirit</title>
        '           <author>Qi, Tianayang</author>
        '           <link>link of each spirit</link>
        '           <description>detail description of each spirit</description>
        '       </item>      
        '       <item>
        '           <title>title of each spirit</title>
        '           <author>Qi, Tianayang</author>
        '           <link>link of each spirit</link>
        '           <description>detail description of each spirit</description>
        '       </item>
        '   </channel>
        '</rss>
        'The follow statment is to realize the RSS function, and I have put the details comment after each statement


        'First of all, to make the website looks like a RSS website
        Response.Clear()
        Response.ContentType = "application/rss+xml"                                            'Make the page looks like the RSS
        Dim xmlStatement As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)             'Define the XML standard
        xmlStatement.WriteStartDocument()
        xmlStatement.WriteStartElement("rss")
        xmlStatement.WriteAttributeString("version", "2.0")
        xmlStatement.WriteStartElement("channel")                                               'Those statement to make the RSS can show the information. Moreover, for this line, it can make the RSS logo show on the top-left in the page

        'Connect to the databse
        Dim sql As String = "SELECT * FROM SpiritList"                                          'Select all item form the SpiritList Database
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SpiritConnString").ConnectionString)      'Connect to the database
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()                                                                             'Open the database
        Dim oleDbReader As OleDbDataReader = cmd.ExecuteReader()                                'Make the oleDbReader can read the information for Database. 'ExecutReader()' mean it will just read the information for database, and not change anything

        'Define the elemnt of XML
        xmlStatement.WriteElementString("title", "Chinese Spirit and Liquired")                 'Define the <title> element, this <title> means the RSS' title
        xmlStatement.WriteElementString("link", "http://localhost:58219/List.aspx")             'Define the <link> element. Make it link to Home Page of this website
        xmlStatement.WriteElementString("description", "Enjoy youself with this website, Thank you!")   'Define the <description> element. Change the information show in this element
        xmlStatement.WriteElementString("language", "en-us")                                    'Define the <language> element

        'Get the information for databse, and put those information into 
        Do While oleDbReader.Read()                                                             'This is a loop statment, which mean if oleDbReader has next elemt, then execute the following statment
            xmlStatement.WriteStartElement("item")                                              'This statement can make RSS show the information for the Database
            xmlStatement.WriteElementString("title", oleDbReader("spiritName").ToString())      'Define the <title> element. Make it to show the information in the Database
            xmlStatement.WriteElementString("author", "Qi, Tianyang")                           'Define the <author> element. Make it to show my name 'Qi, Tianyang'
            xmlStatement.WriteElementString("link", oleDbReader("manuWebsite").ToString())      'Define the <link> element and make it linkaboe. It will take user to the each spirit's offical website
            xmlStatement.WriteStartElement("description")                                       'Define the <description> element. Make it to show all imformation which is stored in the database
            xmlStatement.WriteString("Price: " & oleDbReader("spiritPrice").ToString())
            xmlStatement.WriteString("   Persentage Alochol: " & oleDbReader("percentageAlochol").ToString())
            xmlStatement.WriteString("   Produced In: " & oleDbReader("producedIn").ToString())
            xmlStatement.WriteString("   Manufacturer: " & oleDbReader("manufacturer").ToString() & vbNewLine)
            xmlStatement.WriteString("Star: " & oleDbReader("star").ToString() & vbNewLine)
            xmlStatement.WriteString("Detail: " & oleDbReader("detail").ToString())
            xmlStatement.WriteEndElement()
            xmlStatement.WriteEndElement()
        Loop

        xmlStatement.WriteEndElement()
        xmlStatement.WriteEndElement()
        xmlStatement.WriteEndDocument()
        xmlStatement.Flush()
        xmlStatement.Close()
        Response.End()
    End Sub

End Class